$(function() {
	// $('#delModal').modal('hide');
	$(".uploadBtn").click(function() {
		$("#file").click();
	});
	$.ajax({
		url:"../../../hivelearn/topic/preAdd.json?_t="+new Date().getTime()+"&groupId="+groupId,
		dataType:"json",
		success:function(data){
			if(data.data.topicType&&data.data.topicType==1){
				$("#topic-type").hide();
			}
			token = data.tokenmessage.token;
		}
	})
});
var token;
var hash = window.location.hash.slice(1);
var groupId = hash.split(",")[0];
var queryOrgId = hash.split(",")[1];

function saveTopic() {
	var topicTitle = $("#topicTitle").val()?$("#topicTitle").val():"未定义";
	var topicCover ="";
	if($("[name='topicCover']").val()){
		topicCover = $("[name='topicCover']").val();
	}
	$.ajax({
		url : '../../../hivelearn/topic/saveTopic.json',
		method:"post",
		data : {
			"groupId" : groupId,
			"topicTitle":topicTitle,
			"topicCover":topicCover,
			"topicType":$("[name='topicType']:checked").val(),
			"ORCHID_WEB_TOKEN":token
		},
		dataType : "json",
		success : function(data) {
			window.location.href="../../../hivelearnweb/portal/meetingtheme/meetingTheme.html#"+groupId+","+queryOrgId;
		}
	})
}
function toList(){
	window.location.href="../../../hivelearnweb/portal/meetingtheme/meetingTheme.html#"+groupId+","+queryOrgId;
}
function uploadFile() {
/*	$.ajax({
		url : "../../../hivelearn/topic/uploadImage.json",
		data : new FormData($('#uploadForm')[0]),
		method : "post",
		processData : false,
		contentType : false,
		cache : false,
		success : function(data) // 服务器成功响应处理函数
		{
			$("#topicCover").attr(
					"src",
					"../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
							+ data.data.attachid);
			$('[name="topicCover"]').val(data.data.attachid);
			console.log("上传成功");
			$("#file").val("");

		},
		error : function(data)// 服务器响应失败处理函数
		{
			console.log("服务器异常");
			$("#file").val("");
		}
	});*/
	 $("#uploadForm").ajaxSubmit({
	    	url: "../../../hivelearn/topic/uploadImage.json?_t="+new Date().getTime(),
	        data:$("#uploadForm").serialize(),
	        dataType:"json",
	        success:function(data){
	        	$("#topicCover").attr(
						"src",
						"../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
								+ data.attachid);
				$('[name="topicCover"]').val(data.attachid);
				console.log("上传成功");
				$("#file").val("");
			},
			error:function(data){
				console.log("服务器异常");
				$("#file").val("");
			}
	    });
}
//检查
/*function checkfile(target) {
	if(!$(target).val()){
		return false;
	}
	var isIE = /msie/i.test(navigator.userAgent) && !window.opera; 
	var fileSize = 0;
	var filetypes = [ ".jpg", ".png", ".gif"];
	var filepath = target.value;
	var filemaxsize = 1024 * 2;// 2M
	if (filepath) {
		var isnext = false;
		var fileend = filepath.substring(filepath.indexOf("."));
		if (filetypes && filetypes.length > 0) {
			for ( var i = 0; i < filetypes.length; i++) {
				if (filetypes[i] == fileend) {
					isnext = true;
					break;
				}
			}
		}
		if (!isnext) {
			$("#msgModal").find(".msg").text("请选择.png，.jpg，.gif图片文件上传");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
	} else {
		return false;
	}
	if (isIE && !target.files) {
		var filePath = target.value;
		var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
		if (!fileSystem.FileExists(filePath)) {
			$("#msgModal").find(".msg").text("图片不存在，请重新输入！");
			$("#msgModal").modal("show");
			return false;
		}
		var file = fileSystem.GetFile(filePath);
		fileSize = file.Size;
		ieUpload(target,"uploadForm",uploadFile,filemaxsize);
	} else {
		fileSize = target.files[0].size;
		var size = fileSize / 1024;
		if (size > filemaxsize) {
			$("#msgModal").find(".msg").text("图片大小不能大于" + filemaxsize / 1024 + "M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		if (size <= 0) {
			$("#msgModal").find(".msg").text("图片大小不能为0M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		uploadFile();
	}
}
*/